package com.example.server_rpc.controller;

import com.example.server_rpc.config.RabbitConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RpcServer {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @RabbitListener(queues = RabbitConfig.RPC_MSG_SEND_QUEUE) //监听的是client发送的队列（扔消息到此）
    public void process(Message message){
        byte[] clientMsg = message.getBody();
        Message replyMsg = MessageBuilder.withBody((("I am server, having received the message: ") + new String(clientMsg)).getBytes()).build();
        CorrelationData correlationData = new CorrelationData(message.getMessageProperties().getCorrelationId());
        System.out.println(correlationData);
        rabbitTemplate.sendAndReceive(RabbitConfig.RPC_EXCHANGE_NAME,RabbitConfig.RPC_MSG_RECV_QUEUE,replyMsg,correlationData);
    }

}
